package org.raymond.iworks.study.basic.designpattern.mediator;

import java.util.HashMap;
import java.util.Map;

/**
 * @author raymond
 * @version V1.0
 * @Description: TODO
 */
public class ConcreteMediator extends Mediator {
    private Map<String, Colleague> colleagueMap;
    private Map<String, String> interMap;

    public ConcreteMediator() {
        colleagueMap = new HashMap<>();
        interMap = new HashMap<>();
    }

    @Override
    public void register(String name, Colleague colleague) {
        colleagueMap.put(name, colleague);
        if(colleague instanceof Alarm){
            interMap.put("Alarm", name);
        }else if(colleague instanceof CoffeeMachine){
            interMap.put("CoffeeMachine", name);
        }else if(colleague instanceof TV){
            interMap.put("TV", name);
        }else if(colleague instanceof Curtains){
            interMap.put("Curtains", name);
        }
    }

    /**
     * 根据得到的消息,协调各个同事对象完成任务
     * @param stateChange
     * @param name
     */
    @Override
    public void getMessage(int stateChange, String name) {
        if(colleagueMap.get(name) instanceof Alarm){
            if(stateChange==0){
                ((CoffeeMachine)colleagueMap.get(interMap.get("CoffeeMachine"))).startCoffee();
                ((TV)colleagueMap.get(interMap.get("TV"))).startTV();
            }else if(stateChange==1){
                ((TV)colleagueMap.get(interMap.get("TV"))).stopTV();
            }
        }else if(colleagueMap.get(name) instanceof CoffeeMachine){
            ((Curtains)colleagueMap.get(interMap.get("Curtains"))).upCurtains();
        }else if(colleagueMap.get(name) instanceof TV){
        }else if(colleagueMap.get(name) instanceof Curtains){
        }
    }

    @Override
    public void sendMessage() {

    }
}
